<?php

class TestController extends Controller_Action {
    
    private $_testsDir;
    private $_suite;
    
    
    public function indexAction() {
        $timer  = new Benchmark_Timer;
        $this->_suite = new PHPUnit2_Framework_TestSuite();

        $this->_testsDir = 'classes'.DIRECTORY_SEPARATOR.'Test';
        $dir = new DirectoryIterator($this->_testsDir);
        $this->_processDir($dir);

        $timer->start();
        $result = $this->_suite->run();
        $timer->stop();
        $timeElapsed = $timer->timeElapsed();

        $printer = new PHPUnit2_TextUI_ResultPrinter();
        ob_start();
        echo '<pre>';
        $printer->printResult($result, $timeElapsed);
        echo '</pre>';
        $this->testsResults = ob_get_clean();

    }
    
    private function _processDir($dir) {
        foreach($dir as $dirEntry) {
            if (preg_match('/^\./', $dir->getFilename())) continue;
            if ($dir->isDir()) {
                $this->_processDir(new DirectoryIterator($dir->getPathname()));
            } else {
                $this->_suite->addTestSuite(
                                            str_replace(
                                                        array('.php', 'classes'.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), 
                                                        array('', '', '_'), 
                                                        $dir->getPathname()
                                                       )
                                           );
            }
        }
    }

}

?>